{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bc730e0a",
   "metadata": {},
   "source": [
    "------\n",
    "* 2022-11-01 week 10\n",
    "* python课堂笔记\n",
    "* 记录人:梁颖\n",
    "------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43baf478",
   "metadata": {},
   "source": [
    "# 本周学习目标\n",
    "> 1. 字典的方法\n",
    "> 2. 实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a5bab4c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_list = {\n",
    "    1800001:\"Jack\",\n",
    "    1800002:\"Marry\",\n",
    "    1800003:\"Alex\",\n",
    "    1800004:\"小明\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "798a1193",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001: 'Jack', 1800002: 'Marry', 1800003: 'Alex', 1800004: '小明'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(user_list,type(user_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "32af6817",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "0",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43muser_list\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[0;31mKeyError\u001b[0m: 0"
     ]
    }
   ],
   "source": [
    "user_list[0] # 证明字典是无序的，没办法用索引取值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "956a607a",
   "metadata": {},
   "source": [
    "# 1. 字典的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab3af4df",
   "metadata": {},
   "source": [
    "## 1.1 数据结构的常见方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6059eedd",
   "metadata": {},
   "source": [
    "* len()查看长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8a1399b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(user_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "468914f7",
   "metadata": {},
   "source": [
    "* in 查询成员是否存在: 查询的是key是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8998c38f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1800001 in user_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4eb3ca53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jack'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list[1800001]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f091a443",
   "metadata": {},
   "source": [
    "## 1.2 字典的一些方法\n",
    "\n",
    "> 1. 增删改查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8c506e72",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_list = {\n",
    "    1800001:\"Jack\",\n",
    "    1800002:\"Marry\",\n",
    "    1800003:\"Alex\",\n",
    "    1800004:\"小明\"\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c5c5803",
   "metadata": {},
   "source": [
    "* 字典增加\n",
    "> 1. 先查询要增加的key是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a9a90b5a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800001: 'Jack',\n",
       " 1800002: 'Marry',\n",
       " 1800003: 'Alex',\n",
       " 1800004: '小明',\n",
       " 1800005: '李华'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "213acb59",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该用户已存在，请更换key\n"
     ]
    }
   ],
   "source": [
    "# 可以避免替换其他的key:value!\n",
    "if 1800005 in user_list:\n",
    "    print(\"该用户已存在，请更换key\")\n",
    "else:\n",
    "    user_list[1800005] = \"李华\"\n",
    "    print(\"增加成功\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b6839ca",
   "metadata": {},
   "source": [
    "* 字典的修改\n",
    "> 1. 先查询要修改的key是否存在\n",
    "> 2. 如果不存在该key，可能需要考虑是否新增用户"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "979a0f51",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信息更新成功～\n"
     ]
    }
   ],
   "source": [
    "if 1800001 in user_list:\n",
    "     user_list[1800001] = \"Jan\"\n",
    "     print(\"信息更新成功～\")\n",
    "else:\n",
    "    print(\"该key找不到～\")\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6fe9c24c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800001: 'Jan',\n",
       " 1800002: 'Marry',\n",
       " 1800003: 'Alex',\n",
       " 1800004: '小明',\n",
       " 1800005: '李华'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59f3836d",
   "metadata": {},
   "source": [
    "* 字典的查询： get() 可以查询到该key的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0f7c0592",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print( user_list.get(1800009))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "848fae6b",
   "metadata": {},
   "source": [
    "## 1.3 字典的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "03fdbdf0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800001: 'Jan',\n",
       " 1800002: 'Marry',\n",
       " 1800003: 'Alex',\n",
       " 1800004: '小明',\n",
       " 1800005: '李华'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "188cae85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([(1800001, 'Jan'), (1800002, 'Marry'), (1800003, 'Alex'), (1800004, '小明'), (1800005, '李华')])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.items()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e3a8a79",
   "metadata": {},
   "source": [
    "* 字典的循环遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "df23d7f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 回顾：关于序列类型的数据循环\n",
    "strings = \"hello world~\"\n",
    "lists = [18,19,18,20,19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "832c1166",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n",
      " \n",
      "w\n",
      "o\n",
      "r\n",
      "l\n",
      "d\n",
      "~\n"
     ]
    }
   ],
   "source": [
    "for i in strings:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "23c96abd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "19\n",
      "18\n",
      "20\n",
      "19\n"
     ]
    }
   ],
   "source": [
    "for i in lists:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "9c6b9b4b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1800001\n",
      "1800002\n",
      "1800003\n",
      "1800004\n",
      "1800005\n"
     ]
    }
   ],
   "source": [
    "for i in user_list:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "f7c0ae45",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_list = {\n",
    "    1800001:\"Jack\",\n",
    "    1800002:\"Marry\",\n",
    "    1800003:\"Alex\",\n",
    "    1800004:\"小明\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "04cfbc1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1800001 Jack\n",
      "1800002 Marry\n",
      "1800003 Alex\n",
      "1800004 小明\n"
     ]
    }
   ],
   "source": [
    "# 字典的循环\n",
    "for k,v in user_list.items():\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "3af942d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys([1800001, 1800002, 1800003, 1800004])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# dict.keys():取出全部的key\n",
    "user_list.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "020a7633",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['Jack', 'Marry', 'Alex', '小明'])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# dict.values():取出全部的values\n",
    "user_list.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "17ee1300",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1800004, '小明')"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# popitem()随即删除一个\n",
    "user_list.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "f518abcb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jack'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.pop(1800001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "391cad86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800002: 'Marry', 1800003: 'Alex'}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bca6c49",
   "metadata": {},
   "source": [
    "* fromkeys\n",
    "* setdefault"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "30ae2f2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 复位，重启，重置\n",
    "help(user_list.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "a6360d36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Marry': 100, 'Alex': 100}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重置系统人物的属性，所有人族hpl0\n",
    "user_list.values()\n",
    "re_game = {}\n",
    "re_game.fromkeys(user_list.values(),100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "ba4d9ba8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(user_list.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "8a444015",
   "metadata": {},
   "outputs": [],
   "source": [
    "books = {'runoob':'菜鸟教程','google':'Goole 搜索'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "ef0552c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'菜鸟教程'"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "books.setdefault('runoob','菜鸟教程')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b262c13f",
   "metadata": {},
   "outputs": [],
   "source": [
    "books.setdefault('alibaba','阿里阅读')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
